/**
 * Copyright(c) kyle.
 */
package org.example.jmh;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.profile.StackProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * SynchronizedVsLockVsAtomicInteger
 *
 * @author kyle
 * @version 1.00.00
 * @updateRemark [对比几种int加法性能]
 * @date [2021年9月11日17:13:24]
 * @since [2021年9月11日17:13:17]
 *
 *
 * Benchmark                                        Mode  Cnt  Score   Error  Units
 * SynchronizedVsLockVsAtomicInteger.atomic         avgt   10  0.177 ± 0.023  us/op
 * SynchronizedVsLockVsAtomicInteger.lock           avgt   10  0.191 ± 0.006  us/op
 * SynchronizedVsLockVsAtomicInteger.sync           avgt   10  0.271 ± 0.020  us/op
 *
 * 从结果上看新能最好的是AtomicInteger，最差的是synchronized关键字。
 *
 * AtomicInteger的线程堆栈
 * ....[Thread state distributions]....................................................................
 *  99.8%         RUNNABLE
 *   0.2%         WAITING
 *
 * 显式锁Lock的线程堆栈
 * ....[Thread state distributions]....................................................................
 *  86.3%         WAITING
 *  13.7%         RUNNABLE
 *
 *  synchronized关键字的线程堆栈
 *  ....[Thread state distributions]....................................................................
 *  88.9%         BLOCKED
 *  11.0%         RUNNABLE
 *
 *  从堆栈优化上看，AtomicInteger没有Blocked的状态，runnable状态非常高，因此拥有了高性能
 */
@Fork(0)
@Warmup(iterations = 10)
@Measurement(iterations = 10)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Timeout(time = 10, timeUnit = TimeUnit.SECONDS)
public class SynchronizedVsLockVsAtomicInteger {

    public static void main(String[] args) {
        final Options opt = new OptionsBuilder()
                .include(SynchronizedVsLockVsAtomicInteger.class.getSimpleName())
                .addProfiler(StackProfiler.class)
                .build();
        try {
            new Runner(opt).run();
        } catch (RunnerException e) {
            e.printStackTrace();
        }
    }

    @GroupThreads(10)
    @Group("sync")
    @Benchmark
    public void synInc(IntMonitor monitor) {
        monitor.synInc();
    }

    @GroupThreads(10)
    @Group("lock")
    @Benchmark
    public void lockInc(IntMonitor monitor) {
        monitor.lockInc();
    }

    @GroupThreads(10)
    @Group("atomic")
    @Benchmark
    public void inc(AtomicIntegerMonitor monitor) {
        monitor.inc();
    }

    @State(Scope.Group)
    public static class IntMonitor {
        private final Lock lock = new ReentrantLock();
        private int x;

        public void lockInc() {
            lock.lock();
            try {
                x++;
            } finally {
                lock.unlock();
            }
        }

        public void synInc() {
            synchronized (this) {
                x++;
            }
        }
    }

    @State(Scope.Group)
    public static class AtomicIntegerMonitor {
        private final AtomicInteger x = new AtomicInteger();

        public void inc() {
            x.incrementAndGet();
        }
    }
}
